﻿// COPYRIGHT 2012, Justin Michalczak
//
// This file is licenced to the public under the Apache
// License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopperVineSoftware.Stackr.Parser.Lexemes
{
    /// <summary>A conditional statement testing for equality.</summary>
    /// <remarks>
    ///     This statement will pop one value off the stack.
    ///     It will then evaluate whether that value is equal to the top value of the stack.
    /// </remarks>
    public class EqualsConditional : Conditional
    {
        private readonly IEnumerable<Statement> trueBranch;
        private readonly IEnumerable<Statement> falseBranch;

        /// <summary>Sequence of statements to execute if this condition evaluates to <c>true</c>.</summary>
        public IEnumerable<Statement> TrueBranch
        {
            get
            {
                return this.trueBranch;
            }
        }

        /// <summary>Sequence of statements to execute if this condition evaluates to <c>false</c>.</summary>
        public IEnumerable<Statement> FalseBranch
        {
            get
            {
                return this.falseBranch;
            }
        }

        /// <summary>Constructor.</summary>
        /// <param name="trueBranch">Statements to execute when the condition evaluates to <c>true</c>.</param>
        /// <param name="falseBranch">Statements to execute when the condition evaluates to <c>false</c>.</param>
        /// <exception cref="ArgumentNullException">If the <paramref name="trueBranch"/> parameter is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">If the <paramref name="falseBranch"/> parameter is <c>null</c>.</exception>
        public EqualsConditional(IEnumerable<Statement> trueBranch, IEnumerable<Statement> falseBranch)
        {
            if (trueBranch == null)
                throw new ArgumentNullException("trueBranch");
            if (falseBranch == null)
                throw new ArgumentNullException("falseBranch");

            this.trueBranch = trueBranch;
            this.falseBranch = falseBranch;
        }

        /// <summary>Retrieves a string representation of this equals conditional.</summary>
        /// <returns>A string representation of this equals conditional.</returns>
        public override string ToString()
        {
            return string.Format("=? {{ {0} }} {{ {1} }}",
                string.Join(" ", this.trueBranch.Select<Statement, string>((l) => l.ToString())),
                string.Join(" ", this.falseBranch.Select<Statement, string>((l) => l.ToString())));
        }
    }
}
